<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>
<%@ taglib prefix="cfn" uri="http://francetelecom.com/cfn"%>
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/fn"%>
<%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%>

<%-- Creates a form select or list or radio buttons list or check boxes list. Parameters are :
    - code : the label code for the input
    - path : the path for the input
    - valuesVarName : the name of the request scoped variable storing the possible values.
    - disabledValuesVarName : optional, the name of the request scoped variable storing the values
                      among the possible ones that must be disabled.
    - id : optional, the ID for the component enclosing the form element(s).
    - type : optional, "list" for a select, "box" for checkboxes or radio buttons. By default : "list".
    - multiple : optional, whether multiple elements can be selected. By default : false.
    - focused : optional, value must be "focused" to focus the input
    - mandatory : optional, boolean string value, 'true' if the field is mandatory.
    - onchange : optional, the javascript to execute when the value is modified.
    - fixedWidth : optional, only for the boxes case. A boolean value used to specify if
                      the radio buttons or the check boxes width should be the same for
                      all of them. By default : false.
    - addAllValue : optional, boolean string value, 'true' if a value with the label "All"
                      should be added. Only used if the "multiple" option value is 'true'.
    - addNoneValue : optional, boolean string value, 'true' if a value with the label "None"
                      should be added.
    - warningCodes : the label codes for the warning messages. The separator is : ','.
    - showWarning : optional, boolean string value, 'true' to display the first warning message.
    - modifCheck : optional, boolean string value, 'true' to activate the support for the
        form submit button state dynamic check : the submit button is enabled only if changes
        occured on the form.
--%>
<spring:theme var="emptyComboItemLabel" code="search.form.combo.item.empty"/>

<%-- Getting the label for the all value, if necessary --%>
<c:set var="addAllValue" value="${param.addAllValue == 'true'}"/>
<c:if test="${addAllValue}">
  <spring:theme var="allValueLabel" code="all.option"/>
</c:if>

<%-- Getting the label for the none value, if necessary --%>
<c:set var="addNoneValue" value="${param.addNoneValue == 'true'}"/>
<c:if test="${addNoneValue}">
  <spring:theme var="noneValueLabel" code="none.option"/>
</c:if>

<%-- Computing the javascript code to use when the value changes, so
  that the form submit button state is modified --%>
<c:if test="${param.modifCheck == 'true' ? true : false}">
  <c:set var="modifCheckJs" value="checkModified(this);"/>
</c:if>

<%-- Computing the values to disable --%>
<c:if test="${not empty param.disabledValuesVarName}">

  <%-- Building the string concatenation of all the possible values --%>
  <c:forEach items="${requestScope[param.disabledValuesVarName]}" var="disVal">
    <c:set var="disabledValues" value="${disabledValues}${disVal};"/>
  </c:forEach>
</c:if>

<%-- The header --%>
<c:if test="${not empty param.code}">
<jsp:include page="form-field-header.jsp">
  <jsp:param name="code" value="${param.code}"/>
  <jsp:param name="mandatory" value="${param.mandatory}"/>
</jsp:include>
</c:if>

<%-- Displaying the form field --%>
<span id="${param.id}">
  <c:choose>
    <c:when test="${param.type == 'box'}">

      <%-- The boxes case --%>
      <c:choose>
        <c:when test="${param.multiple == 'true'}">

          <%-- The check boxes case --%>
          <span class="${param.fixedWidth == 'true' ? 'fixedWidthRadioBox' : 'basicCheckBoxes'}">
            <c:forEach items="${requestScope[param.valuesVarName]}" var="curItem" varStatus="stat">
              <span>

                <%-- Checking whether the box should be disabled --%>
                <c:set var="curVal" value="${curItem};"/>
                <c:set var="disabled" value="${not empty disabledValues &&
                    fn:contains(disabledValues, curVal)}"/>

                <%-- The box --%>
                <form:checkbox label="${cfn:formatExt(pageContext, curItem, true)}"
                  path="${param.path}" value="${curItem}" cssErrorClass="error ${disabled ? 'disabled' : ''}"
                  onchange="${modifCheckJs}${param.onchange}" onclick="${modifCheckJs}${param.onchange}"
                  cssClass="${stat.first ? param.focused : ''} ${disabled ? 'disabled' : ''}"
                  disabled="${disabled}"/>
              </span>
            </c:forEach>
          </span>

        </c:when>
        <c:otherwise>

          <%-- The radio buttons case --%>
          <span class="${param.fixedWidth == 'true' ? 'fixedWidthRadioBox' : 'basicRadioBts'}">

            <%-- Adding all other values --%>
            <c:forEach items="${requestScope[param.valuesVarName]}" var="curItem">
              <span>
                <%-- Checking whether the radio button should be disabled --%>
                <c:set var="curVal" value="${curItem};"/>
                <c:set var="disabled" value="${not empty disabledValues &&
                    fn:contains(disabledValues, curVal)}"/>

                <%-- The radio button --%>
                <form:radiobutton label="${cfn:formatExt(pageContext, curItem, true)}"
                  path="${param.path}" value="${curItem}" cssErrorClass="error ${disabled ? 'disabled' : ''}"
                  cssClass="radio ${stat.first ? param.focused : ''} ${disabled ? 'disabled' : ''}"
                  onchange="${modifCheckJs}${param.onchange}" onclick="${modifCheckJs}${param.onchange}"
                  disabled="${disabled}"/>
              </span>
            </c:forEach>

            <%-- Adding the "None" value, if necessary --%>
            <c:if test="${addNoneValue}">
              <span>
                <form:radiobutton label="${noneValueLabel}" cssErrorClass="error"
                    path="${param.path}" cssClass="radio"
                    onchange="${modifCheckJs}${param.onchange}" onclick="${modifCheckJs}${param.onchange}"/>
              </span>
            </c:if>

            <%-- Adding the "All" value, if necessary --%>
            <c:if test="${addAllValue}">
              <span>
                <form:radiobutton label="${allValueLabel}" cssErrorClass="error"
                    path="${param.path}" cssClass="radio"
                    onchange="${modifCheckJs}${param.onchange}" onclick="${modifCheckJs}${param.onchange}"/>
              </span>
            </c:if>

          </span>

        </c:otherwise>
      </c:choose>
    </c:when>
    <c:otherwise>

      <%-- The select case --%>

      <%-- Computing the javascript code to use when the value changes, so
        that the form submit button state is modified --%>
      <c:if test="${param.modifCheck == 'true' ? true : false}">
        <c:set var="modifCheckJs" value="var options = $(this).find('option');"/>
        <c:set var="modifCheckJs" value="${modifCheckJs}options.each(function(){checkModified(this);});"/>
      </c:if>

      <%-- The select --%>
      <c:set var="multiple" value="${param.multiple == 'true'}"/>

     <form:select path="${param.path}" cssClass="${param.focused} ${multiple ? 'listSelect' : ''}"
       	cssErrorClass="error ${multiple ? 'listSelect' : ''}"
        onchange="${modifCheckJs}${param.onchange}" multiple="${multiple ? 'multiple' : ''}">

        <%-- Adding the "None" value, if necessary --%>
        <c:if test="${addNoneValue}">
          <form:option label="${noneValueLabel}" value=""/>
        </c:if>

        <%-- Adding the "All" value, if necessary --%>
        <c:if test="${addAllValue}">
          <form:option label="${allValueLabel}" value=""/>
        </c:if>

        <%-- Adding all other values --%>
        <c:forEach items="${requestScope[param.valuesVarName]}" var="curItem">

          <%-- Checking whether the option should be disabled --%>
          <c:set var="curVal" value="${curItem};"/>
          <c:set var="disabled" value="${not empty disabledValues &&
              fn:contains(disabledValues, curVal)}"/>

          <%-- The option --%>
          <form:option value="${curItem}" label="${cfn:formatExt(pageContext, curItem, true)}"
              disabled="${disabled}" cssClass="${disabled ? 'disabled' : ''}"/>
        </c:forEach>
      </form:select>

    </c:otherwise>
  </c:choose>
</span>

<%-- Displaying the form field error message --%>
<jsp:include page="form-field-errors.jsp">
  <jsp:param name="path" value="${param.path}"/>
</jsp:include>

<%-- The row footer --%>
<c:if test="${not empty param.code}">
<jsp:include page="form-field-footer.jsp">
  <jsp:param name="warningCodes" value="${param.warningCodes}"/>
  <jsp:param name="showWarning" value="${param.showWarning}"/>
</jsp:include>
</c:if>
